package com.album.activity;




import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.album.R;
import com.album.common.Constants;
import com.album.utils.CommonUtil;
import com.album.utils.MyLog;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.Button;


public class SettingActivity extends Activity {

	Button btn0;
	Button btn1;
	Button btn2;
	Button btn3;
	Button btn4;
	Button btn5;
	Button btn6;
	Button btn7;
	Button btn8;
	Button btn9;
	SharedPreferences preference;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		btn0=(Button) findViewById(R.id.btn0);
		btn1=(Button) findViewById(R.id.btn1);
		btn2=(Button) findViewById(R.id.btn2);
		btn3=(Button) findViewById(R.id.btn3);
		btn4=(Button) findViewById(R.id.btn4);
		btn5=(Button) findViewById(R.id.btn5);
		btn6=(Button) findViewById(R.id.btn6);
		btn7=(Button) findViewById(R.id.btn7);
		btn8=(Button) findViewById(R.id.btn8);
		btn9=(Button) findViewById(R.id.btn9);
		btn0.setOnClickListener(listener);
		btn1.setOnClickListener(listener);
		btn2.setOnClickListener(listener);
		btn3.setOnClickListener(listener);
		btn4.setOnClickListener(listener);
		btn5.setOnClickListener(listener);
		btn6.setOnClickListener(listener);
		btn7.setOnClickListener(listener);
		btn8.setOnClickListener(listener);
		btn9.setOnClickListener(listener);
		
		preference = getSharedPreferences("preference",Context.MODE_PRIVATE);
		 
				boolean login_status = preference.getBoolean(Constants.LOGIN_STATUS, false);
				setBgfirst(login_status, btn0, btn1);
				saveKeyfirst(Constants.LOGIN_STATUS, login_status);

				boolean config_changes = preference.getBoolean(Constants.CONFIG_CHANGES, false);
				setBgfirst(config_changes, btn2, btn3);
				saveKeyfirst(Constants.CONFIG_CHANGES, config_changes);

				boolean network = preference.getBoolean(Constants.NETWORK_STATUS, true);
				setBgfirst(network, btn4, btn5);
				saveKeyfirst(Constants.NETWORK_STATUS, network);

//				boolean num4 = preference.getBoolean("num4", false);
//				setBgfirst(num4, btn6, btn7);
//				saveKeyfirst("num4", num4);

//				boolean num5 = preference.getBoolean("num5", false);
//				setBgfirst(num5, btn8, btn9);
//				saveKeyfirst("num5", num5);
		
    }
	
	 OnClickListener listener=new OnClickListener(){  
       @Override  
       public void onClick(View v){
    	   int i=0;
    	   switch (v.getId()) {
			case R.id.btn0:
				i=1;
				break;
			case R.id.btn1:
				i=1;
				break;
			case R.id.btn2:
				i=2;
				break;
			case R.id.btn3:
				i=2;
				break;
			case R.id.btn4:
				i=3;
				break;
			case R.id.btn5:
				i=3;
				break;
			case R.id.btn6:
				i=4;
				break;
			case R.id.btn7:
				i=4;
				break;
			default:
				break;
    	   }
			setSetingValue(i);
       }
	 }; 
	 public void setSetingValue(int ds){
		 boolean value = false;
		 String num = null;
		switch (ds) {
		case 1:		//是否自动登录
			 value = preference.getBoolean(Constants.LOGIN_STATUS, false);
			 num = Constants.LOGIN_STATUS;
			 saveSetting(1,num,value, btn0, btn1);
			break;
		case 2:		//是否横竖屏切换
			 value = preference.getBoolean(Constants.CONFIG_CHANGES, false);
			 num = Constants.CONFIG_CHANGES;
			 saveSetting(2,num,value, btn2, btn3);
			 break;
		case 3:		//无WIFI 是否提示
			 value = preference.getBoolean(Constants.NETWORK_STATUS, false);
			 num = Constants.NETWORK_STATUS;
			 saveSetting(3,num,value, btn4, btn5);
			 break;	
		case 4:		//是否自动更新
			 value = preference.getBoolean(Constants.UPDATE_AUTO, false);
			 num = Constants.UPDATE_AUTO;
			 saveSetting(4,num,value, btn6, btn7);
			 break;
//		case 5:
//			 value = preference.getBoolean("num5", false);
//			 setBg(value, btn8, btn9);
//			 num ="num5"; 
		default:
			break;
		}
		saveKey(num, value);
	 } 
          /**
           * 
			 CommonUtil.setPreferences(Constants.LOGIN_STATUS, flgs, this);
           * @param selectId
           * @param flgs
           * @param begin
           * @param end
           */
	public void saveSetting(int selectId,String saveKey,boolean flgs,Button begin,Button end){
		if(flgs){ 
			begin.setBackgroundResource(R.drawable.set_off);
			end.setBackgroundResource(R.drawable.set_on); 
		}
		else{
			begin.setBackgroundResource(R.drawable.set_on);
			end.setBackgroundResource(R.drawable.set_off);
		}
		MyLog.debug(getClass(), "saveKey:"+saveKey+" flgs:"+flgs);
	}
	 public String getUserAgent()
	    {
//	     String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
//		 String userAgentString = WebSettings.getUserAgentString();
//		 Websettings.getUserAgent();
	     return null;
	    } 
	public void setBgfirst(boolean flgs,Button begin,Button end){
		if(flgs){
			begin.setBackgroundResource(R.drawable.set_on);
			end.setBackgroundResource(R.drawable.set_off); 
		}
		else{
			begin.setBackgroundResource(R.drawable.set_off);
			end.setBackgroundResource(R.drawable.set_on);
		}
	}
     public void saveKeyfirst(String key,boolean value){ 
    	 if(preference==null){
    		preference = getSharedPreferences("preference",Context.MODE_PRIVATE);
    	 }
		 Editor edit = preference.edit();
		 edit.putBoolean(key, value);
		 edit.commit();
	 }
     public void saveKey(String key,boolean value){ 
    	 if(preference==null){
    		preference = getSharedPreferences("preference",Context.MODE_PRIVATE);
    	 }
		 Editor edit = preference.edit();
		 if(value){
			 edit.putBoolean(key, false);
		 }else{
			 edit.putBoolean(key, true);
		 }
		 edit.commit();

			boolean network = CommonUtil.getPreferences(this, Constants.LOGIN_STATUS);
			MyLog.debug(getClass(), "*******network:"+network);
	 }
}